package org.cainiao.basic.reactive.mock;

import reactor.core.publisher.Flux;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

public class MyHttpRequest implements Supplier<Flux<Integer>> {

    final AtomicInteger transientHelper = new AtomicInteger();

    public Flux<Integer> get() {
        return Flux.generate(sink -> {
            int i = transientHelper.getAndIncrement();
            if (i == 10) {
                sink.next(i);
                sink.complete();
            } else if (i % 3 == 0) {
                sink.next(i);
            } else {
                sink.error(new IllegalStateException("Transient error at " + i));
            }
        });
    }

}
